package org.glut.saltedfish.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {
    //创建单一全局变量，多线程安全
    public static SqlSessionFactory sessionFactory = null;
    static {
        String config = "mybatis-config.xml";

        try {
            InputStream is = Resources.getResourceAsStream(config);

            sessionFactory = new SqlSessionFactoryBuilder().build(is);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取 sqlsession
     * @return
     */
    public static SqlSession getSession() {
        return sessionFactory.openSession();
    }

    public static SqlSessionFactory getSessionFctory() {
        return sessionFactory;
    }
}
